/*!	 cellrenderer_time.cpp
**	 Template File
**
**	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
**	Copyright (c) 2008 Chris Moore
**
**	This package is free software; you can redistribute it and/or
**	modify it under the terms of the GNU General Public License as
**	published by the Free Software Foundation; either version 2 of
**	the License, or (at your option) any later version.
**
**	This package is distributed in the hope that it will be useful,
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**	General Public License for more details.
**
*/

#ifdef USING_PCH
#	include "pch.h"
#else
#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include <synfig/general.h>

#include <gtkmm/entry.h>
#include <gtkmm/button.h>
#include "cellrenderer_time.h"
#include "app.h"
#include "widgets/widget_time.h"

#include <gui/localization.h>

#endif

using namespace std;
// using namespace etl;
using namespace synfig;
using namespace studio;

CellRenderer_Time::CellRenderer_Time():
    Glib::ObjectBase(typeid(CellRenderer_Time)),
    Gtk::CellRendererText(),
    property_time_(*this, "time", synfig::Time(0)),
    property_fps_(*this, "fps", float(0))
{
    CellRendererText::signal_edited().connect(sigc::mem_fun(*this, &studio::CellRenderer_Time::string_edited_));
}

CellRenderer_Time::~CellRenderer_Time()
{
    if (getenv("SYNFIG_DEBUG_DESTRUCTORS")) {
        synfig::info("CellRenderer_Time::~CellRenderer_Time(): Deleted");
    }
}

void
CellRenderer_Time::string_edited_(const Glib::ustring&path, const Glib::ustring&str)
{
    signal_edited_(path, Time((String)str, (Real)Time(property_fps_)));
}

void
CellRenderer_Time::render_vfunc(
    const ::Cairo::RefPtr< ::Cairo::Context>& cr,
    Gtk::Widget& widget,
    const Gdk::Rectangle& background_area,
    const Gdk::Rectangle& cell_area,
    Gtk::CellRendererState flags)
{
    if (!cr) {
        return;
    }

    const Time time(property_time_);
    const float fps((Real)Time(property_fps_));

    property_text() = (Glib::ustring)time.get_string(fps, App::get_time_format());

    CellRendererText::render_vfunc(cr, widget, background_area, cell_area, flags);
}

Gtk::CellEditable*
CellRenderer_Time::start_editing_vfunc(
    GdkEvent* event,
    Gtk::Widget& widget,
    const Glib::ustring& path,
    const Gdk::Rectangle& background_area,
    const Gdk::Rectangle& cell_area,
    Gtk::CellRendererState flags)
{
    // If we aren't editable, then there is nothing to do
    if (!property_editable()) {
        return 0;
    }

    const Time time(property_time_);
    const float fps((Real)Time(property_fps_));

    property_text() = (Glib::ustring)time.get_string(fps, App::get_time_format() | Time::FORMAT_FULL);
    return CellRendererText::start_editing_vfunc(event, widget, path, background_area, cell_area, flags);
}